﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Text;

namespace Actuals.Data
{
    public class DalCompany : Dal<ICompany, Company>, IDalCompany
    {

         private readonly IDalManager _manager;
        private static readonly IDalCompany _instance = new DalCompany();
        private const string SP_GET_Company = "usp_GetCompany";
        private const string SP_GET_COMPANIES  = "usp_GetCompanies";

        private const string SP_ADD_COMPANY  = "usp_AddCompany";
        private const string SP_UPDATE_COMPANY = "usp_UpdateCompany";
        private const string SP_DELETE_COMPANY = "usp_DeleteCompany";


            
        public static IDalCompany Instance
        {
            get { return _instance; }
        }

        public DalCompany()
        {
            _manager = new DalManager();
        }

        public DalCompany(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(ICompany obj, DataRow row)
        {
            obj.CompanyId = _manager.GetValue<int>(row, "CompanyId");
            obj.CompanyName = _manager.GetValue<string>(row, "CompanyName");
            obj.EmailId = _manager.GetValue<string>(row, "EmailId");
            obj.Password = _manager.GetValue<string>(row, "Password");
            
        }
        public ICompany GetCompany(int companyId)
        {
            _manager.StoredProcedureName = SP_GET_Company;
            _manager.AddInParameter("@ProjectId", DbType.Int32, companyId);

            ICompany company = Convert(_manager.ExecuteDataSet());


            return company;
        }

        public IList<ICompany> GetCompanies()
        {
            _manager.StoredProcedureName = SP_GET_COMPANIES;
            

            List<ICompany> companies = new List<ICompany>();
            foreach (ICompany company in ConvertAll(_manager.ExecuteDataSet()))
                companies.Add(company);

            return companies;

           
        }
        public void AddCompany(ICompany company)
        {
            _manager.StoredProcedureName = SP_ADD_COMPANY;
            //_manager.AddInParameter("@CompanyId", DbType.Int32, company.CompanyId);
            _manager.AddInParameter("@EmailId", DbType.String, company.EmailId);
            _manager.AddInParameter("@Password", DbType.String, company.Password);
            
            _manager.ExecuteNonQuery();
            //return true;
        }

        public void UpdateCompany(ICompany company)
        {
            _manager.StoredProcedureName = SP_UPDATE_COMPANY;
            _manager.AddInParameter("@CompanyId", DbType.Int32, company.CompanyId);
            _manager.AddInParameter("@EmailId", DbType.String, company.EmailId);
            _manager.AddInParameter("@Password", DbType.String, company.Password);
            _manager.ExecuteNonQuery();
            //return true;

        }

        public void DeleteCompany(int companyId)
        {
            _manager.StoredProcedureName = SP_DELETE_COMPANY;
            _manager.AddInParameter("@CompanyId", DbType.Int32, companyId);
            

            _manager.ExecuteNonQuery();

            //return true;
        }

    }
}
